/**
* initCart
* 初始化购物车模块
*/
function initCart()
{ 
  jQuery(".showflowcat font").html("加载中...");
  jQuery.ajax({
     type: 'POST',
     url: '/index.php/Order/Cart/queryCart',
     dataType: 'json',
     success: function(data){
      if(data.count>0)
      {
        jQuery(".showflowcat font").html(data.count);
      }
      else
      {
        jQuery(".showflowcat font").html('0');
      }
     }
  })
}

var add_cart_flag = 1;
function addToCartUser(gid)
{
  if(add_cart_flag!=1)
  {
    //alert("不要重复点击");
    return false;
  }
  add_cart_flag = 0;
  var spec_id = jQuery('input:radio[name="spec_'+gid+'"]:checked').val();
  var qty = document.forms['ECS_FORMBUY'].elements['number'].value;
  if(qty>2)
  {
    alert("最多只能购买2件");
    document.forms['ECS_FORMBUY'].elements['number'].value = 2;
    add_cart_flag = 1;
    return false;
  }

  jQuery.ajax({
     type: 'POST',
     url: '/index.php/Order/Cart/addCart',
     data: { fid: spec_id,num: qty,gid:gid},
     dataType: 'json',
     success: function(data){
      if(data.error)
      {
        add_cart_flag = 1;
        alert(data.msg);
      }
      else
      {
        jQuery(".showflowcat font").html(data.count);
        initCart();
        window.location.href="/index.php/Order/Cart/Index";
      }
     }
  })
}

function jsfy(shipid,obj){
  if (jQuery(obj).is(':checked')) {
      var fee = jQuery(".ship_fee_"+shipid).html();
      jQuery("#shipFee").val(fee);
  }

  jsOrderTotal();
}
function jsOrderTotal()
{
  //商品金额
  var goodsAmount = jQuery('#goods_amount').val();
  //运费
  var shipFee = jQuery("#shipFee").val();
  if(shipFee<0)
  {
    alert('运费不能为负数');
    return false;
  }
  //账户余额
  var userAccount = jQuery("#userAccount").val();
  var orderTotal = parseFloat(goodsAmount)+parseFloat(shipFee);

  jQuery("#orderTotalHtml").html("&yen;"+parseFloat(goodsAmount)+"元&nbsp; + &nbsp;运费:"+"&yen;"+parseFloat(shipFee)+"元");
  jQuery("#yfTotalHtml").html("&yen;"+parseFloat(parseFloat(goodsAmount)+parseFloat(shipFee))+"元");

  jQuery("#ECS_SURPLUS").val(parseFloat(goodsAmount)+parseFloat(shipFee));
  jQuery("#orderTotal").val(parseFloat(goodsAmount)+parseFloat(shipFee));
  jQuery("#yfTotal").val(parseFloat(goodsAmount)+parseFloat(shipFee));

  if(userAccount<orderTotal)
  {
    alert("用户余额不足");
    return false;
  }
  if(orderTotal<0)
  {
    alert('再好好算算吧，优惠完要付的钱都成负数了！');
    jsOrderTotal();
    return false;
  }
}

function submitOrder()
{
  //检查配送地址是否选择
  var address = jQuery(".address:checked").val();
  if(address=='0')
  {
    var consignee = jQuery("#consignee").val();
    var address = jQuery("#address").val();
    var phone_tel = jQuery("#phone_tel").val();
    var fhr_phone = jQuery("#fhr_phone").val();
    var fhr_name = jQuery("#fhr_name").val();

    if(consignee=='')
    {
      alert("收货人不能为空");
      return false;
    }
    if(address=='')
    {
      alert("收货地址不能为空");
      return false;
    }
    if(phone_tel=='')
    {
      alert("收货人电话不能为空");
      return false;
    }
    if(fhr_name=='')
    {
      alert("发货人不能为空");
      return false;
    }
    if(fhr_phone=='')
    {
      alert("发货人电话不能为空");
      return false;
    }
  }
  else
  {
    jsOrderTotal();
  }
  //判断配送方式是否选择
  var shipping = jQuery(".shipping:checked").val();
  if(shipping)
  {
    jsOrderTotal();
  }
  else
  {
    alert("选择配送方式");
    return false;
  }

  jQuery("#theForm").submit();
}